MapService.Control.MeasureDistance = OpenLayers.Class(OpenLayers.Control.Measure, {
	type           : OpenLayers.Control.TYPE_TOOL,
	persist        : true,
	immediate      : true,
	geodesic       : true,
	textNodes      : null,
	pointNodes     : null,
	
	initialize     : function(options){
		var me = this;
		
		me.textNodes  = [];
		me.pointNodes = [];
		
		var pointStyle = {
			pointRadius        : 4,
			graphicName        : "square",
			fillColor          : "white",
			fillOpacity        : 1,
			strokeWidth        : 1,
			strokeOpacity      : 1,
			strokeColor        : "#333333",
			fontColor          : "#FF0000",
			fontSize           : "11px",
			fontFamily         : "Arial",
			fontWeight         : "bold",
			labelAlign         : "lm",
			labelOutlineWidth  : 1,
			labelOutlineColor  : "#FFFFFF"
		};
		
		var lineStyle = {
			strokeWidth   : 3,
			strokeOpacity : 1,
			strokeColor   : "#666666"
		};
		
		var polygonStyle = {
			strokeWidth    : 2,
			strokeOpacity  : 1,
			strokeColor    : "#666666",
			fillColor      : "white",
			fillOpacity    : 0.3
		};
		
		var rule = new OpenLayers.Rule({
        	symbolizer: {
        		"Point"   : pointStyle,
        		"Line"    : lineStyle,
        		"Polygon" : polygonStyle
        	}
        });
		
		var style = new OpenLayers.Style(null, {
			rules: [rule]
		});
			
		me.handlerOptions = {
			layerOptions : {
				styleMap: new OpenLayers.StyleMap({
					"default": style
				})
			}
		};
		

		OpenLayers.Control.Measure.prototype.initialize.apply(this, [OpenLayers.Handler.Path, options]);
		
		me.displayClass = 'ux-icon-measure-distance';
		me.events.register('measure', null, me.onMeasure);
		me.events.register('measurepartial', null, me.onMeasurePartial);
	},
	
	onMeasure : function(evt){
		var me = this;
		
		var geometry = evt.geometry;
		var total = me.getBestLength(geometry);
		
		var pointStyle = {
			fontColor          : "#FF0000",
			fontSize           : "11px",
			fontFamily         : "Arial",
			fontWeight         : "bold",
			labelAlign         : "lt",
			labelOutlineWidth  : 1,
			labelOutlineColor  : "#FFFFFF"
		};
		
		me.handler.point.style = pointStyle;
		me.handler.point.style.label = "Total: "+total[0].toFixed(2) + " " + total[1];
		me.textNodes.push(me.handler.point);
		me.handler.layer.addFeatures([me.handler.point]);
	},
	
	onMeasurePartial : function(evt){
		var me = this;
		
		var geometry = evt.geometry;
		
		var total = this.getBestLength(geometry);
		
		var pointStyle = {
			pointRadius       : 4,
			graphicName       : "square",
			fillColor         : "white",
			fillOpacity       : 1,
			strokeWidth       : 1,
			strokeOpacity     : 1,
			strokeColor       : "#333333",
			fontColor         : "#FF0000",
			fontSize          : "11px",
			fontFamily        : "Arial",
			fontWeight        : "bold",
			labelAlign        : "lm",
			labelOutlineWidth : 1,
			labelOutlineColor : "#FFFFFF",
			label             : "Total: "+total[0].toFixed(2) + " " + total[1]
		};
		me.handler.point.style = pointStyle;
		
		if(me.textNodes.length >= geometry.components.length || total[0] == 0){
			me.handler.layer.removeAllFeatures();
			me.textNodes = [];
			me.pointNodes = [];
		};
		
		if(geometry.components.length - me.pointNodes.length >= 2){
			var c = geometry.components[geometry.components.length-2];
			var pointNodeStyle = {
				pointRadius    : 4,
				graphicName    : "square",
				fillColor      : "white",
				fillOpacity    : 1,
				strokeWidth    : 1,
				strokeOpacity  : 1,
				strokeColor    : "#333333"
			};
			var pointNode = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(c.x, c.y),{},pointNodeStyle);
			me.pointNodes.push(pointNode);
			me.handler.layer.addFeatures([pointNode]);
		};
		
		var len = geometry.components.length;
        var from = geometry.components[len - 2];
        var to = geometry.components[len - 1];
        var ls = new OpenLayers.Geometry.LineString([from, to]);
        var dist = me.getBestLength(ls);
        if (!dist[0]) {
            return;
        }
        var label = dist[0].toFixed(2) + " " + dist[1];
        var textNode = this.textNodes[len - 2] || null;
        if (textNode && !textNode.layer) {
        	me.textNodes.pop();
            textNode = null;
        }
        if (!textNode) {
            var c = ls.getCentroid();
            var textNodeStyle = {
            	fontColor           : "#000000",
            	fontSize            : "10px",
            	fontFamily          : "Arial",
            	labelAlign          : "cm",
            	labelOutlineWidth   : 2,
            	labelOutlineColor   : "#FFFFFF"
            };
            textNode = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(c.x, c.y),{},textNodeStyle);
            me.textNodes.push(textNode);
            me.handler.layer.addFeatures([textNode]);
        }
        textNode.geometry.x  = (from.x + to.x) / 2;
        textNode.geometry.y  = (from.y + to.y) / 2;
        textNode.style       = textNode.style || {};
        textNode.style.label = label;
        textNode.layer.drawFeature(textNode);
	},
	
	CLASS_NAME: "MapService.Control.MeasureDistance"
});